package org.zhouhe.web.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class SimplePage extends TagSupport {
	private static final long serialVersionUID = 1L;
	private String width;
	private List<String> buttons;
	private List<String> functions;
	private int submitCheckedIndex = 0;
	
	@Override
	public int doStartTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			//写入Layout布局DIV开始标签
			out.println("<div class='easyui-layout' data-options='collapsible:true,fit:true'>");
		} catch (IOException e) {
			e.printStackTrace();
		}
		buttons = new ArrayList<String>();
		functions = new ArrayList<String>();
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		try {
			//写入Layout布局DIV结束标签
			out.println("</div>");
			out.println();
			//写入按钮标签：
			if(buttons.size() > 0){
				out.println("<div id='"+ id +"_buttons'>");
				for(String btn: buttons){
					out.println("	"+ btn);
				}
				out.println("</div>");
			}
			//写入事件：
			if(functions.size()>0){
				out.println("<script type='text/javascript'><!--");
				for(String fun : functions){
					out.println(fun);
				}
				out.println("//--></script>");
			}
			out.flush();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return EVAL_PAGE;
	};
	
	public void addButton(String btn){
		this.buttons.add(btn);
	}
	
	public void addFunction(String function){
		functions.add(function);
	}

	public String getWidth() {
		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	/**
	 * 获得序列值，方法执行后，该值自动加1
	 * @return
	 */
	public int newSubmitCheckedIndex() {
		return submitCheckedIndex++;
	}

}
